package com.health.controller.permission;

import com.alibaba.dubbo.config.annotation.Reference;
import com.health.constant.MessageConstant;
import com.health.entity.PageResult;
import com.health.entity.QueryPageBean;
import com.health.entity.Result;
import com.health.pojo.Permission;
import com.health.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @Classname PermissionController
 * @Description TODO
 * @Version 1.0.0
 * @Date 2023/3/18 16:25
 * @Author RainGrd
 */
@RestController
@Slf4j
@RequestMapping("/permission")
public class PermissionController {


    @Reference
    private PermissionService permissionService;


    @RequestMapping("/findPage.do")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        log.info("queryPageBean:{}", queryPageBean);
        return permissionService.queryPermissionPage(queryPageBean);
    }

    @RequestMapping("/add.do")
    public Result add(@RequestBody Permission permission, Integer[] roleIds) {
        log.info("要添加的权限对象:{}", permission);
        log.info("要添加的角色权限:{}", Arrays.asList(roleIds));
        return permissionService.savePermission(permission, roleIds);
    }


    @RequestMapping("/edit.do")
    public Result edit(@RequestBody Permission permission, Integer[] roleIds) {
        log.info("要修改的权限对象:{}", permission);
        log.info("要修改的角色权限:{}", Arrays.asList(roleIds));
        return permissionService.editPermission(permission, roleIds);
    }

    @RequestMapping("/findRoleIdsByMenuId.do")
    public Result findRoleIdsByMenuId(@Param("permissionId") Integer permissionId) {
        log.info("userId:{}", permissionId);
        List<Integer> userIds = permissionService.queryRoleIdsByPermissionId(permissionId);
        log.info("menuIds:{}", userIds);
        if (userIds != null && userIds.size() > 0) {
            return Result.success(userIds, MessageConstant.QUERY_CHECKGROUP_SUCCESS);
        }
        return Result.error(MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

    @RequestMapping("/delete.do")
    public Result delete(@RequestParam("id") Integer permissionId) {
        log.info("要删除的预约管理id:{}", permissionId);
        return permissionService.clearPermissionId(permissionId);
    }

}
